package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.*;
import com.j48.entrancegurad.model.Character;
import com.j48.entrancegurad.service.*;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.*;

@Controller
public class MainController {
    @Autowired
    private IAdminService adminservice;
    @Autowired
    private ICharacterService characterService;
    @Autowired
    private IA_CService a_cService;
    @Autowired
    private IPowerService powerService;
    @Autowired
    private IP_CService p_cService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRecordService recordService;
    @Autowired
    private IVisitorService visitorService;
    @Autowired
    private IPictureService pictureService;
    @Autowired
    private IHostService hostService;
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IOpinionService opinionService;

    /**
     * 初始页面设置
     * @return
     */
    @RequestMapping("/")
    @ResponseBody
    public ModelAndView index(){
       return loginofall();
    }

    /**
     * 登录页面设置
     * @return
     */
    @RequestMapping("/loginofall")
    @ResponseBody
    public ModelAndView loginofall(){
        return new ModelAndView("jsp/loginofall");
    }

    /**
     * 管理员登录验证
     * @param admin
     * @param request
     * @return 登录成功与否
     */
    @RequestMapping("/login")
    @ResponseBody
    public String login(Admin admin, HttpServletRequest request,Notice notice){
        String admin_username=request.getParameter("admin_username");
        String admin_password=request.getParameter("admin_password");
        admin.setAdmin_username(admin_username);
        admin.setAdmin_password(admin_password);
        if(adminservice.login(admin)!=null&&adminservice.login2(admin).getAdmin_login()!=1){
            Admin admin1=adminservice.login2(admin);
            A_C a_c=a_cService.select(admin1);
            if(a_c!=null) {
                Timestamp ts=new Timestamp(new Date().getTime());
                admin1.setAdmin_lasttime(ts);
                adminservice.updateLasttime(admin1);
                adminservice.updataLogin(admin1);
                request.getSession().setAttribute("admin",admin1);
                Character character = characterService.select(a_c);
                ArrayList<P_C> p_cs = p_cService.select(character);
                ArrayList<Power> powers = powerService.select(p_cs);
                HashMap<Integer, Power> mainMenus = powerService.MainMenu(powers);
                Set<Integer> keys = mainMenus.keySet();
                ArrayList<String> menu = new ArrayList<>();
                for (Integer k : keys) {
                    Power p = mainMenus.get(k);
                    menu.add(p.getPower_name());
                }
                request.getSession().setAttribute("mainMenus", menu);
                ArrayList<Character> list = characterService.selectcharacterall();
                request.getSession().setAttribute("rolelist", list);
                //登录时查询全部历史记录，存入session
                List<Record> recordslist = new ArrayList<Record>();
                recordslist = recordService.selectAll();
                List<Integer> recorduser_idList = new ArrayList<Integer>();
                List<User> recorduserlist = new ArrayList<User>();
                List<Host> recordhostList=new ArrayList<Host>();
                if(recordslist!=null) {
                    for (Record r : recordslist) {
                        recorduser_idList.add(r.getRecord_user_id());
                    }
                    for (int i : recorduser_idList) {
                        User user = new User();
                        user.setUser_id(i);
                        user = userService.selectByUser_id(user);
                        recorduserlist.add(user);
                    }
                    for (User u : recorduserlist) {
                        Host host = new Host();
                        host.setHost_id(u.getUser_host_id());
                        host = hostService.selectById(host);
                        recordhostList.add(host);
                    }
                }
                request.getSession().setAttribute("recordList", recordslist);
                request.getSession().setAttribute("recordUserList", recorduserlist);
                request.getSession().setAttribute("recordHostList", recordhostList);
                List<Admin> adminList=new ArrayList<Admin>();
                adminList = adminservice.selectAll();
                List<Integer> adminaidList=new ArrayList<>();
                List<Integer> admincidList=new ArrayList<>();
                List<Character> admincharacterList=new ArrayList<>();
                if(adminList!=null) {
                    for (Admin a : adminList) {
                        int i = a.getAdmin_id();
                        adminaidList.add(i);
                    }
                    for (int i:adminaidList){
                        Admin admin0=new Admin();
                        admin0.setAdmin_id(i);
                        A_C a_c0=a_cService.select(admin0);
                        admincidList.add(a_c0.getA_c_character_id());
                    }
                    for (int i:admincidList) {
                        Character character0=new Character();
                        character0.setCharacter_id(i);
                        character0=characterService.selectById(character0);
                        admincharacterList.add(character0);
                    }
                }
                request.getSession().setAttribute("adminCharacter",admincharacterList);
                request.getSession().setAttribute("admin1",adminList);
//                ArrayList<Host> hostList=new ArrayList<Host>();
//                hostList=hostService.selectAll();
//                request.getSession().setAttribute("host",hostList);
                ArrayList<User> userlist=new ArrayList<User>();
                ArrayList<Integer> userId=new ArrayList<Integer>();
                ArrayList<Host> hostlist=new ArrayList<Host>();
                Host host=new Host();
                userlist=userService.selectAll();
                for(int i=0;i<userlist.size();i++){
                    userId.add(i,userlist.get(i).getUser_host_id());
                }
                for(int i=0;i<userId.size();i++){
                        host.setHost_id(userId.get(i));
                    hostlist.add(i,hostService.selectById(host));
                }
                ArrayList<Notice> noticelist=new ArrayList<Notice>();
                noticelist=noticeService.selectAll();
                ArrayList<Integer> adminid=new ArrayList<Integer>();
                ArrayList<String> noticeNames=new ArrayList<String>();
                Admin noticeAdmin=new Admin();
                Admin noticeAdmin1=new Admin();
                for(int i=0;i<noticelist.size();i++){
                    adminid.add(i,noticelist.get(i).getNotice_admin_id());
                }
                for(int i=0;i<adminid.size();i++){
                    noticeAdmin.setAdmin_id(adminid.get(i));
                    noticeAdmin1=adminservice.selectById(noticeAdmin);
                    noticeNames.add(noticeAdmin1.getAdmin_name());
                }
                ArrayList<Opinion> opinionlist=new ArrayList<Opinion>();
                ArrayList<Opinion> opinionlistAll=new ArrayList<Opinion>();
                ArrayList<User> useropinionlist=new ArrayList<User>();
                ArrayList<Host> hostopinionlist=new ArrayList<Host>();
                ArrayList<Admin> adminopinionlist=new ArrayList<Admin>();
                ArrayList<User> useropinionlist1=new ArrayList<User>();
                ArrayList<Host> hostopinionlist1=new ArrayList<Host>();
                ArrayList<Admin> adminopinionlist1=new ArrayList<Admin>();
                ArrayList<Integer> useridlist=new ArrayList<Integer>();
                ArrayList<Integer> adminidlist=new ArrayList<Integer>();
                ArrayList<Integer> hostidlist=new ArrayList<Integer>();
                ArrayList<Integer> useridlist1=new ArrayList<Integer>();
                ArrayList<Integer> adminidlist1=new ArrayList<Integer>();
                ArrayList<Integer> hostidlist1=new ArrayList<Integer>();
                User useropinion =new User();
                Host hostopinion=new Host();
                Admin adminopinion=new Admin();
                User useropinion1 =new User();
                Host hostopinion1=new Host();
                Admin adminopinion1=new Admin();
                opinionlist=opinionService.selectAllNotRead();
                opinionlistAll=opinionService.selectAll();
                if(opinionlistAll!=null) {
                    for (int i = 0; i < opinionlistAll.size(); i++) {
                        useridlist1.add(i, opinionlistAll.get(i).getOpinion_user_id());
                        adminidlist1.add(i, opinionlistAll.get(i).getOpinion_admin_id());
                    }
                    for (int i = 0; i < useridlist1.size(); i++) {
                        adminopinion1.setAdmin_id(adminidlist1.get(i));
                        adminopinionlist1.add(i, adminservice.selectById(adminopinion1));
                    }
                    for (int i = 0; i < adminidlist1.size(); i++) {
                        useropinion1.setUser_id(useridlist1.get(i));
                        useropinionlist1.add(i, userService.selectByUser_id(useropinion1));
                    }
                    for (int i = 0; i < useropinionlist1.size(); i++) {
                        hostidlist1.add(i, useropinionlist1.get(i).getUser_host_id());
                    }
                    for (int i = 0; i < hostidlist1.size(); i++) {
                        hostopinion1.setHost_id(hostidlist1.get(i));
                        hostopinionlist1.add(i, hostService.selectById(hostopinion1));
                    }

                    for (int i = 0; i < opinionlist.size(); i++) {
                        useridlist.add(i, opinionlist.get(i).getOpinion_user_id());
                        adminidlist.add(i, opinionlist.get(i).getOpinion_admin_id());
                    }
                    for (int i = 0; i < useridlist.size(); i++) {
                        adminopinion.setAdmin_id(adminidlist.get(i));
                        adminopinionlist.add(i, adminservice.selectById(adminopinion));
                    }
                    for (int i = 0; i < adminidlist.size(); i++) {
                        useropinion.setUser_id(useridlist.get(i));
                        useropinionlist.add(i, userService.selectByUser_id(useropinion));
                    }
                    for (int i = 0; i < useropinionlist.size(); i++) {
                        hostidlist.add(i, useropinionlist.get(i).getUser_host_id());
                    }
                    for (int i = 0; i < hostidlist.size(); i++) {
                        hostopinion.setHost_id(hostidlist.get(i));
                        hostopinionlist.add(i, hostService.selectById(hostopinion));
                    }
                }
                request.getSession().setAttribute("opinionlistAll",opinionlistAll);
                request.getSession().setAttribute("opinionlist",opinionlist);
                request.getSession().setAttribute("useropinionlist",useropinionlist);
                request.getSession().setAttribute("hostopinionlist",hostopinionlist);
                request.getSession().setAttribute("adminopinionlist",adminopinionlist);
                request.getSession().setAttribute("useropinionlist1",useropinionlist1);
                request.getSession().setAttribute("hostopinionlist1",hostopinionlist1);
                request.getSession().setAttribute("adminopinionlist1",adminopinionlist1);
                Collections.reverse(noticelist);
                Collections.reverse(noticeNames);
                request.getSession().setAttribute("noticelist",noticelist);
                request.getSession().setAttribute("noticeNames",noticeNames);
                request.getSession().setAttribute("hostlistall",hostlist);
                request.getSession().setAttribute("userlist",userlist);
                List<Visitor> visitorList = new ArrayList<Visitor>();
                visitorList=visitorService.selectAllAble();
                List<User> visitorUserList = new ArrayList<User>();
                List<Host> visitorHostList=new ArrayList<Host>();
                if(visitorList!=null) {
                    List<Integer> visitor_user_idList = new ArrayList<Integer>();
                    for (Visitor v : visitorList) {
                        visitor_user_idList.add(v.getVisitor_user_id());
                    }
                    for (int i : visitor_user_idList) {
                        User user = new User();
                        user.setUser_id(i);
                        user = userService.selectByUser_id(user);
                        visitorUserList.add(user);
                    }
                    for (User u:visitorUserList) {
                        Host visitorhost=new Host();
                        visitorhost.setHost_id(u.getUser_host_id());
                        visitorhost=hostService.selectById(visitorhost);
                        visitorHostList.add(visitorhost);
                    }
                }
                request.getSession().setAttribute("visitorList", visitorList);
                request.getSession().setAttribute("visitorUserList", visitorUserList);
                request.getSession().setAttribute("visitorHostList",visitorHostList);
                List<Visitor> visitorListAll = new ArrayList<Visitor>();
                visitorListAll=visitorService.selectAll();
                List<User> visitorUserListAll = new ArrayList<User>();
                List<Admin> visitorAdminList=new ArrayList<Admin>();
                List<Host> visitorHostListAll = new ArrayList<Host>();
                if(visitorListAll!=null) {
                    List<Integer> visitor_user_idListAll = new ArrayList<Integer>();
                    List<Integer> admin_idList=new ArrayList<Integer>();
                    for (Visitor v : visitorListAll) {
                        visitor_user_idListAll.add(v.getVisitor_user_id());
                        admin_idList.add(v.getVisitor_admin_id());
                    }
                    for (int i : visitor_user_idListAll) {
                        User user = new User();
                        user.setUser_id(i);
                        user = userService.selectByUser_id(user);
                        visitorUserListAll.add(user);
                    }
                    for (int i:admin_idList) {
                        Admin visitoradmin=new Admin();
                        visitoradmin.setAdmin_id(i);
                        visitoradmin=adminservice.selectById(visitoradmin);
                        visitorAdminList.add(visitoradmin);
                    }
                    for (User u:visitorUserListAll) {
                        Host visitorhostAll=new Host();
                        visitorhostAll.setHost_id(u.getUser_host_id());
                        visitorhostAll=hostService.selectById(visitorhostAll);
                        visitorHostListAll.add(visitorhostAll);
                    }
                }
                request.getSession().setAttribute("visitorAllList", visitorListAll);
                request.getSession().setAttribute("visitorAllUserList", visitorUserListAll);
                request.getSession().setAttribute("visitorAllAdminList",visitorAdminList);
                request.getSession().setAttribute("visitorAllHostList",visitorHostListAll);
                List<Picture> pictureList=new ArrayList<Picture>();
                pictureList=pictureService.selectAllAble();
                request.getSession().setAttribute("pictureList",pictureList);
                List<Picture> pictureListAll=new ArrayList<Picture>();
                pictureListAll=pictureService.selectAll();
                request.getSession().setAttribute("pictureListAll",pictureListAll);
                return "true";
            }
        }
        return "false";
    }

    /**
     * 退出登录
     * @param req
     */
    @RequestMapping("/destroyLogin")
    @ResponseBody
    public void updateLogin(HttpServletRequest req){
        Admin admin=new Admin();
       admin=(Admin)req.getSession().getAttribute("admin");
       adminservice.updataLogin1(admin);

    }

    /**
     * 跳转登陆后主页
     * @return
     */
    @RequestMapping("/home")
    @ResponseBody
    public ModelAndView home(){
        return new ModelAndView("jsp/home");
    }

    /**
     * 初始化视图页面
     * @return
     */
    @RequestMapping("/right")
    @ResponseBody
    public ModelAndView right(){
        return new ModelAndView("jsp/right");
    }

    /**
     * 跳转角色管理页面
     * @return
     */
    @RequestMapping("/roleManagement")
    @ResponseBody
    public ModelAndView roleManagement(){
        return new ModelAndView("jsp/roleManagement");
    }

    /**
     * 跳转历史记录页面
     * @return
     */
    @RequestMapping("/record")
    @ResponseBody
    public ModelAndView record(){ return new ModelAndView("jsp/record"); }

    /**
     * 跳转管理员列表页面
     * @return
     */
    @RequestMapping("/admin_index")
    @ResponseBody
    public ModelAndView admin_index(){ return new ModelAndView("jsp/admin_index"); }

    /**
     * 跳转管理员添加页面
     * @return
     */
    @RequestMapping("/adminAdd")
    @ResponseBody
    public ModelAndView adminAdd(){ return new ModelAndView("jsp/adminAdd"); }

    /**
     * 跳转业主页面
     * @return
     */
    @RequestMapping("/host")
    @ResponseBody
    public ModelAndView host(){ return new ModelAndView("jsp/host"); }

    /**
     * 跳转业主添加页面
     * @return
     */
    @RequestMapping("/hostAdd")
    @ResponseBody
    public ModelAndView hostAdd(){ return new ModelAndView("jsp/hostAdd"); }

    /**
     * 跳转公告页面
     * @return
     */
    @RequestMapping("/notice")
    @ResponseBody
    public ModelAndView notice(){ return new ModelAndView("jsp/notice"); }

    /**
     * 跳转公告添加页面
     * @return
     */
    @RequestMapping("/noticeAdd")
    @ResponseBody
    public ModelAndView noticeAdd(){ return new ModelAndView("jsp/noticeAdd"); }

    /**
     * 跳转报修页面
     * @return
     */
    @RequestMapping("/repair")
    @ResponseBody
    public ModelAndView repair(){ return new ModelAndView("jsp/repair"); }

    /**
     * 跳转报修添加页面
     * @return
     */
    @RequestMapping("/repairAdd")
    @ResponseBody
    public ModelAndView repairAdd(){ return new ModelAndView("jsp/repairAdd"); }

    /**
     * 跳转全部报修页面
     * @return
     */
    @RequestMapping("/repairall")
    @ResponseBody
    public ModelAndView repairAll(){ return new ModelAndView("jsp/repairAll"); }

    /**
     * 跳转用户列表页面
     * @return
     */
    @RequestMapping("/user")
    @ResponseBody
    public ModelAndView user(){ return new ModelAndView("jsp/user"); }

    /**
     * 跳转访客列表页面
     * @return
     */
    @RequestMapping("/visitor")
    @ResponseBody
    public ModelAndView visitorList(){ return new ModelAndView("jsp/visitorList"); }

    /**
     * 跳转全部访客列表页面
     * @return
     */
    @RequestMapping("/visitorAll")
    @ResponseBody
    public ModelAndView visitorListAll(){ return new ModelAndView("jsp/visitorListAll"); }

    /**
     * 跳转访客添加页面
     * @return
     */
    @RequestMapping("/visitorAdd")
    @ResponseBody
    public ModelAndView visitorAdd(){ return new ModelAndView("jsp/visitorAdd"); }

    /**
     * 跳转图片页面
     * @return
     */
    @RequestMapping("/pictureList")
    @ResponseBody
    public ModelAndView pictureList(){ return new ModelAndView("jsp/pictureList"); }

    /**
     * 跳转意见列表页面
     * @return
     */
    @RequestMapping("/opinion")
    @ResponseBody
    public ModelAndView opinion(){ return new ModelAndView("jsp/opinion"); }

    /**
     * 跳转全部意见列表页面
     * @return
     */
    @RequestMapping("/opinionAll")
    @ResponseBody
    public ModelAndView opinionAll(){ return new ModelAndView("jsp/opinionAll"); }

    /**
     * 跳转管理员修改页面
     * @return
     */
    @RequestMapping("/adminUpdate")
    @ResponseBody
    public ModelAndView adminUpdate(){ return new ModelAndView("jsp/adminUpdate"); }

    /**
     * 跳转角色添加页面
     * @return
     */
    @RequestMapping("/addrole")
    @ResponseBody
    public ModelAndView createrole(){ return new ModelAndView("jsp/addrole"); }


}
